package org.jeecg.modules.activiti.listener;

import com.alibaba.fastjson.JSON;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.activiti.entity.ActBusiness;
import org.jeecg.modules.activiti.service.Impl.ActBusinessServiceImpl;
import org.jeecg.modules.zcgl.service.ILeaseContractInfoService;
import org.jeecg.modules.zcgl.service.ILeaseContractStandardRentService;
import org.jeecg.modules.zcgl.service.impl.LeaseContractInfoServiceImpl;
import org.jeecg.modules.zcgl.service.impl.LeaseContractStandardRentServiceImpl;
import org.jeecg.modules.zcgl.vo.LeaseContractInfoPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 合同变更审批后监听
 */
@Component
public class LeaseContractEditListener implements ExecutionListener {


    ActBusinessServiceImpl actBusinessService;

    @Autowired
    private ILeaseContractInfoService leaseContractInfoService;
    @Autowired
    private ILeaseContractStandardRentService leaseContractStandardRentService;

    public void notify(DelegateExecution execution) throws Exception {
        if (leaseContractStandardRentService == null){
            leaseContractStandardRentService = (LeaseContractStandardRentServiceImpl)SpringContextUtils.getBean("leaseContractStandardRentServiceImpl");
        }
        if (leaseContractInfoService == null){
            leaseContractInfoService = (LeaseContractInfoServiceImpl)SpringContextUtils.getBean("leaseContractInfoServiceImpl");
        }
        if (actBusinessService == null){
            actBusinessService = (ActBusinessServiceImpl) SpringContextUtils.getBean("actBusinessServiceImpl");
        }

        String eventName = execution.getEventName();
        //start
        if (EVENTNAME_START.equals(eventName)) {
        } else if (EVENTNAME_END.equals(eventName)) {
            ActBusiness actBusiness = actBusinessService.getById(execution.getProcessBusinessKey());
            if (actBusiness != null && "zcgl_lease_contract_info".equalsIgnoreCase(actBusiness.getTableName())){
                LeaseContractInfoPage vo = JSON.parseObject(actBusiness.getData(), LeaseContractInfoPage.class);
                leaseContractInfoService.modify(vo);
            }
        }
//        System.out.println("----" + execution.getProcessBusinessKey() + "" + execution.getProcessDefinitionId());
    }
}
